package org.apache.commons.httpclient;

import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.httpclient.util.EncodingUtil;

public class ChunkedOutputStream extends OutputStream
{
  private static final byte[] CRLF;
  private static final byte[] ENDCHUNK;
  private static final byte[] ZERO;
  private byte[] cache;
  private int cachePosition;
  private OutputStream stream;
  private boolean wroteLastChunk;

  static
  {
    byte[] arrayOfByte1 = new byte[2];
    arrayOfByte1[0] = 13;
    arrayOfByte1[1] = 10;
    CRLF = arrayOfByte1;
    ENDCHUNK = CRLF;
    byte[] arrayOfByte2 = new byte[1];
    arrayOfByte2[0] = 48;
    ZERO = arrayOfByte2;
  }

  public ChunkedOutputStream(OutputStream paramOutputStream)
    throws IOException
  {
    this(paramOutputStream, 2048);
  }

  public ChunkedOutputStream(OutputStream paramOutputStream, int paramInt)
    throws IOException
  {
    this.stream = null;
    this.cachePosition = 0;
    this.wroteLastChunk = false;
    this.cache = new byte[paramInt];
    this.stream = paramOutputStream;
  }

  public void close()
    throws IOException
  {
    finish();
    super.close();
  }

  public void finish()
    throws IOException
  {
    if (this.wroteLastChunk)
      return;
    flushCache();
    writeClosingChunk();
    this.wroteLastChunk = true;
  }

  public void flush()
    throws IOException
  {
    this.stream.flush();
  }

  protected void flushCache()
    throws IOException
  {
    if (this.cachePosition <= 0)
      return;
    byte[] arrayOfByte = EncodingUtil.getAsciiBytes(Integer.toHexString(this.cachePosition) + "\r\n");
    this.stream.write(arrayOfByte, 0, arrayOfByte.length);
    this.stream.write(this.cache, 0, this.cachePosition);
    this.stream.write(ENDCHUNK, 0, ENDCHUNK.length);
    this.cachePosition = 0;
  }

  protected void flushCacheWithAppend(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    byte[] arrayOfByte = EncodingUtil.getAsciiBytes(Integer.toHexString(paramInt2 + this.cachePosition) + "\r\n");
    this.stream.write(arrayOfByte, 0, arrayOfByte.length);
    this.stream.write(this.cache, 0, this.cachePosition);
    this.stream.write(paramArrayOfByte, paramInt1, paramInt2);
    this.stream.write(ENDCHUNK, 0, ENDCHUNK.length);
    this.cachePosition = 0;
  }

  public void write(int paramInt)
    throws IOException
  {
    this.cache[this.cachePosition] = (byte)paramInt;
    this.cachePosition = (1 + this.cachePosition);
    if (this.cachePosition != this.cache.length)
      return;
    flushCache();
  }

  public void write(byte[] paramArrayOfByte)
    throws IOException
  {
    write(paramArrayOfByte, 0, paramArrayOfByte.length);
  }

  public void write(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    if (paramInt2 >= this.cache.length - this.cachePosition)
      flushCacheWithAppend(paramArrayOfByte, paramInt1, paramInt2);
    while (true)
    {
      return;
      System.arraycopy(paramArrayOfByte, paramInt1, this.cache, this.cachePosition, paramInt2);
      this.cachePosition = (paramInt2 + this.cachePosition);
    }
  }

  protected void writeClosingChunk()
    throws IOException
  {
    this.stream.write(ZERO, 0, ZERO.length);
    this.stream.write(CRLF, 0, CRLF.length);
    this.stream.write(ENDCHUNK, 0, ENDCHUNK.length);
  }
}

/* Location:           E:\Desktop\Android\wordstd\classes_dex2jar.jar
 * Qualified Name:     org.apache.commons.httpclient.ChunkedOutputStream
 * JD-Core Version:    0.5.3
 */